import { createRouter, createWebHashHistory,createWebHistory, RouteRecordRaw } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import HomeView from '../views/HomeView.vue'
import UserView from '../views/UserView.vue'
import { checkUserAuth } from '@/utils/auth'
import AdminView from '../views/AdminView.vue'
import LandlordView from '../views/LandlordView.vue'
import ChatView from '../views/ChatView.vue'
import ReportView from '@/views/ReportView.vue'; // 假设报表页面组件为ReportView.vue

// 扩展 RouteMeta 接口
declare module 'vue-router' {
  interface RouteMeta {
    requiresAuth?: boolean // 是否需要身份认证
    roles?: number[] // 允许访问的角色列表
  }
}

// 路由配置
const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: { requiresAuth: false }
  },
  {
    path: '/user',
    name: 'user',
    component: UserView,
    meta: { requiresAuth: true }
  },
  {
    path: '/admin',
    name: 'admin',
    component: AdminView,
    meta: { requiresAuth: true, roles: [1] },
    children: [
      {
        path: 'house',
        name: 'adminHouse',
        component: () => import('../components/AdminHouseComponent.vue'),
        meta: { requiresAuth: true, roles: [1] }
      },
      {
        path: 'manager',
        name: 'adminManager',
        component: () => import('../components/AdminManagerComponent.vue'),
        meta: { requiresAuth: true, roles: [1] }
      },
      {
        path: 'monitor',
        name: 'adminMonitor',
        component: () => import('../components/AdminSystemMonitor.vue'),
        meta: { requiresAuth: true, roles: [1] }
      },
      {
        path: '',
        redirect: '/admin/house'
      }
    ]
  },
  {
    path: '/landlord',
    name: 'landlord',
    component: LandlordView,
    meta: { requiresAuth: true, roles: [3] }
  },
  {
    path: '/chat',
    name: 'chat',
    component: ChatView,
    meta: { requiresAuth: true }
  },
  {
    path: '/report',
    name: 'Report',
    component: ReportView
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局路由守卫
router.beforeEach(async (to, from, next) => {
  // 检查用户认证状态
  //const isAuthenticated = await checkUserAuth()
  const userRole = Number(localStorage.getItem('userRole'))
  const isAuthenticated = userRole !== null && userRole !== undefined && userRole !== 0
  //console.log('userRole:', userRole)
  // 处理需要认证的路由
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
    return
  }
  // 如果已登录用户访问登录或注册页面，重定向到首页
  if ((to.path === '/login' || to.path === '/register') && isAuthenticated) {
    next('/')
    return
  }

  // 处理角色权限
  if (to.meta.roles && !to.meta.roles.includes(userRole)) {
    next('/')
    return
  }

  // 处理根路径访问
  if (to.path === '/') {
    if (userRole === 1) {
      next('/admin')
    } else if (userRole === 3) {
      next('/landlord')
    } else {
      next()
    }
    return
  }

  next()
})

export default router
